package br.com.hstar.client.formapgto;

import java.util.ArrayList;
import java.util.List;

import br.com.hstar.client.ServiceFactory;
import br.com.hstar.client.util.FormPanel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class FormaPgtoEditPanel extends VerticalPanel{

	private Button incluir;
	private Button limpar;
	private TextBox descricao;
	private DockPanel dockPanel;//tabela com o titulo azul
	private VerticalPanel dockPanelContent;//painel central do dockpanel
	private FlexTable table;//tabela que vai conter os listboxes para adicionar os precos
	private HorizontalPanel hp = new HorizontalPanel();//painel que vai conter a tabela
	
	public FormaPgtoEditPanel() {
		dockPanel = new DockPanel();
		dockPanel.setStyleName( "DockPanel" );
		
		//título da tabela
		HTML html = new HTML( "Formas de Pagamento" );
		html.setStyleName( "IntPanel-Title" );
		dockPanel.add( html, DockPanel.NORTH );
		
		//conteúdo do dockPanel
		dockPanelContent = new VerticalPanel();
		dockPanelContent.setStyleName( "ContentTable" );
		dockPanel.add( dockPanelContent, DockPanel.CENTER );
		
		//inicializa os botões
		incluir = new Button("Incluir");
		incluir.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				FormaPgtoUI p = new FormaPgtoUI();
				p.setDescricao(descricao.getText().trim());
				ServiceFactory.createFormaPgtoService().saveOrUpdate(p, new AddFormaPgtoAsyncCallback());
				
				descricao.setText("");
			}
		});
		limpar = new Button("Limpar");
		limpar.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				descricao.setText("");
			}
		});
		
		init();
	}
	
	private void init() {
		table = new FlexTable();
		descricao   = FormPanel.addTextBox("", 30, 30, "");
		
		table.setWidget(0,0,new Label("Forma PGTO: "));
		table.setWidget(0,1,descricao);
		HorizontalPanel but = new HorizontalPanel();
		but.add(incluir);but.add(limpar);
		table.setWidget(1,1,but);
		
		hp.add(table);
		this.add(hp);
		this.add(dockPanel);
		
		//lista todos os preços existentes
		ServiceFactory.createFormaPgtoService().listAll(new ListAllFormaPgtoAsyncCallback());
	}
	
	public void addPgto(final FormaPgtoUI pgto) {
		//adicoina o ítem visual
		//essa implementacao do Image contem o PordutoDO.
		final MyPgtoImage img = new MyPgtoImage( getImageUrl("img/remove.png") );
		img.setPreco(pgto);
		
		final HorizontalPanel vp = new HorizontalPanel();
		vp.add(new HTML("Forma PGTO: "+pgto.getDescricao()));
		vp.add(img);
		
		dockPanelContent.add(vp);
		img.addClickListener(new ClickListener(){
			public void onClick(Widget arg0) {
				if(Window.confirm("Deseja realmente excluir este registro?")) {
					removePgto(vp, pgto);
				}
			}
		});
	}
	public void removePgto(Widget wid, FormaPgtoUI pgto) {
		dockPanelContent.remove(wid);
		ServiceFactory.createFormaPgtoService().delete(pgto.getId(), new RemoveFormaPgtoAsyncCallback());
	}
	
	
	//adiciona o preco novo
	private class AddFormaPgtoAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			Window.alert("erro ao adicionar uma forma de pgto");
		}
		public void onSuccess(Object result) {
			Window.alert("Registro adicionado com sucesso!");
			ServiceFactory.createFormaPgtoService().listAll(new ListAllFormaPgtoAsyncCallback());
		}
	}
	
	//remove o preço da lista e do banco de dados
	private class RemoveFormaPgtoAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			Window.alert("erro ao listar a forma de pgto");
		}
		public void onSuccess(Object result) {
			Window.alert("Registro excluído com sucesso!");
			ServiceFactory.createFormaPgtoService().listAll(new ListAllFormaPgtoAsyncCallback());
		}
	}
	
	//lista todos os precos existentes
	private class ListAllFormaPgtoAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			Window.alert("erro ao listar forma de pgto");
		}
		public void onSuccess(Object result) {
			List pgtos = (ArrayList)result;
			dockPanelContent.clear();
			for (int i = 0; i < pgtos.size(); i++) {
				FormaPgtoUI p = (FormaPgtoUI)pgtos.get(i);
				addPgto(p);
			}
		}
	}
	
	public static String getImageUrl( String relImgUrl ) {
		return GWT.getModuleBaseURL() + relImgUrl;
	}
}